package com.carassistant.beta.home;

import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.carassistant.beta.R;
import com.carassistant.beta.common.AppManager;
import com.carassistant.beta.common.CarAssistantActivity;
import com.carassistant.beta.common.CarAssistantApp;
import com.carassistant.beta.common.dialog.MAlertDialog;
import com.carassistant.beta.compass.CompassMainActivity;
import com.carassistant.beta.dog.DogMainActivity;
import com.carassistant.beta.music.MusicMainActivity;
import com.carassistant.beta.phone.PhoneMainActivity;

/**
 * 主Activity
 * @author Alen
 * */
public class HomeActivity extends CarAssistantActivity {
	/** 1. Music   2.Phone  3.Video   4.Speed  5.Record  6.Navi  7.Compass  8.Voice*/
	private Button iconBtn1,iconBtn2,iconBtn3,iconBtn4,iconBtn5,iconBtn6,iconBtn7,iconBtn8;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Display display = getWindowManager().getDefaultDisplay(); 
		CarAssistantApp.screenMax = display.getWidth() > display.getHeight() ? display.getWidth() : display.getHeight(); 
		CarAssistantApp.screenMin = display.getWidth() < display.getHeight() ? display.getWidth() : display.getHeight();
		
        init();
    }
    
    private void init(){
    	iconBtn1 = (Button) findViewById(R.id.homeIcon1);
    	iconBtn2 = (Button) findViewById(R.id.homeIcon2);
    	iconBtn3 = (Button) findViewById(R.id.homeIcon3);
    	iconBtn4 = (Button) findViewById(R.id.homeIcon4);
    	iconBtn5 = (Button) findViewById(R.id.homeIcon5);
    	iconBtn6 = (Button) findViewById(R.id.homeIcon6);
    	iconBtn7 = (Button) findViewById(R.id.homeIcon7);
    	iconBtn8 = (Button) findViewById(R.id.homeIcon8);
    	iconBtn1.setOnClickListener(this);
    	iconBtn2.setOnClickListener(this);
    	iconBtn3.setOnClickListener(this);
    	iconBtn4.setOnClickListener(this);
    	iconBtn5.setOnClickListener(this);
    	iconBtn6.setOnClickListener(this);
    	iconBtn7.setOnClickListener(this);
    	iconBtn8.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
    	super.onClick(v);
    	switch (v.getId()) {
		case R.id.homeIcon1:  //Music
			goOtherActivity(MusicMainActivity.class);
			break;
		case R.id.homeIcon2:  //Phone
			goOtherActivity(PhoneMainActivity.class);
			break;
		case R.id.homeIcon3:  //Video
			
			break;
		case R.id.homeIcon4:  //Speed
			goOtherActivity(DogMainActivity.class);
			break;
		case R.id.homeIcon5:  //Record
			
			break;
		case R.id.homeIcon6:  //Navi
			
			break;
		case R.id.homeIcon7:  //Compass
			goOtherActivity(CompassMainActivity.class);
			break;
		case R.id.homeIcon8:  //Voice
			
			break;
		default:
			break;
		}
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(keyCode == KeyEvent.KEYCODE_BACK){
    		exit();
    	}
    	return super.onKeyDown(keyCode, event);
    }
    
    private void exit(){
    	final MAlertDialog dialog = new MAlertDialog(HomeActivity.this, MAlertDialog.DIALOG_TEXT);
    	dialog.setTitle("CarAssistant");
    	dialog.setMessage("您确定要退出吗？");
    	dialog.setNegativeButton(R.string.cancel, new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
    	dialog.setPositiveButton(R.string.ok, new OnClickListener() {
			@Override
			public void onClick(View v) {
				AppManager.getInstance().appExit(HomeActivity.this);
			}
		});
    	dialog.show();
    }
}